package com.taikang.base.controller;

import java.awt.Color;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.taikang.base.constant.SessionConstant;
import com.taikang.base.util.VerifyCode;

/**
 * 会员首页模块
 * @creator     taikang
 * @create-time 2016/3/12 16:10:22
 * @email taikang@taikang.com
 * @company www.taikang.com
 * @version 1.0
 */
@Controller("verCodeController")
@Scope("prototype")
@RequestMapping("/")
public class VerCodeController extends BaseController {

	/**
	 * 生成验证码
	 * 
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/japtcha.htm", method = RequestMethod.GET)
	public void japtcha(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws Exception {
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
		response.addHeader("Cache-Control", "post-check=0, pre-check=0");
		response.setHeader("Pragma", "no-cache");
		response.setContentType("image/jpeg");

		// 生成验证码，写入用户session
		String verifyCode = VerifyCode.generateTextCode(VerifyCode.TYPE_UPPER_ONLY, 4, null);
		session.setAttribute(SessionConstant.JAPTCHACODE, verifyCode);
		logger.debug("验证码:" + verifyCode);
		// 输出验证码给客户端
		ServletOutputStream out = response.getOutputStream();
		BufferedImage bim = VerifyCode.generateImageCode(verifyCode, 90, 30, 3, true, Color.WHITE, Color.BLACK, null);
		ImageIO.write(bim, "JPEG", out);
		try {
			out.flush();
		} finally {
			out.close();
		}
	}
	
}